package xmlparser;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;

import utils.HTML;

public class JdomParser {

    private Document _doc;

    public JdomParser() {
    }

    public JdomParser(String file) {

	try {
	    _doc = new SAXBuilder(false).build(file);
	} catch (JDOMException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    public List<Element> rootGetListOf(String elem) {

	Element root = _doc.getRootElement();
	return root.getChildren("Placemark");
    }

    public List<Element> getListOf(String elem, Element element) {

	return element.getChildren(elem);
    }

    /**
     * Obtains all data from the XML
     * 
     * @return list of strings which every one contains the id of the dock, the
     *         address, the free and occupied slots and the coordinates.
     *         Separated by ':'
     */
    public List<String> parseDocument() {

	String plData;
	List<String> result = new ArrayList<String>();
	Element root = _doc.getRootElement();
	List<Element> placemarks = root.getChildren("Placemark");

	// Iterating through Placemarks
	for (Element placemark : placemarks) {

	    plData = new String();

	    // Getting address and bycicles

	    List<Element> list2 = placemark.getChildren("description");

	    for (Element ee : list2) {

		try {

		    // Saving a temporal file
		    Writer w = new OutputStreamWriter(new FileOutputStream(
			    "tmp.xml"), "UTF-8");
		    w.write(ee.getText());
		    w.close();

		    // Creating new Document
		    Document tmpdoc = new SAXBuilder(false).build("tmp.xml");
		    Element tmproot = tmpdoc.getRootElement();
		    List<Element> div = tmproot.getChildren("div");

		    int control = 0;
		    for (Element etmp : div) {

			List<Content> cont = etmp.getContent();

			for (Content ctmp : cont) {

			    if (ctmp instanceof Text && control != 1) {

				plData += ((Text) ctmp).getText() + ":";
			    }
			}
			if (control == 3)
			    control = 0;
			else
			    control++;
		    }
		    new File("tmp.xml").delete();
		} catch (Exception e) {

		    e.printStackTrace();
		}
	    }

	    // Getting coordinates

	    Element point = placemark.getChild("Point");
	    Element coordSet = point.getChild("coordinates");
	    String coord = coordSet.getText();
	    StringTokenizer st = new StringTokenizer(coord);
	    String lon = st.nextToken(",");
	    String lat = st.nextToken();

	    // TODO there's a coordinate that contains a question mark so we are
	    // ignoring that one.
	    if (!lat.contains("?") && !lon.contains("?")) {
		
		plData += lon + ":" + lat;
		result.add(plData);
	    }
	    
	}

	return result;
    }

    public String getDockState() {

	Element root = _doc.getRootElement();
	List<Element> list = root.getChildren("Placemark");

	for (Element e : list) {

	    List<Element> list2 = e.getChildren("description");

	    for (Element ee : list2) {

		try {
		    Writer w = new OutputStreamWriter(new FileOutputStream(
			    "tmp.xml"), "UTF-8");
		    w.write(ee.getText());
		    w.close();
		    Document tmpdoc = new SAXBuilder(false).build("tmp.xml");
		    Element tmproot = tmpdoc.getRootElement();
		    List<Element> div = tmproot.getChildren("div");
		    int control = 0;
		    for (Element etmp : div) {

			List<Content> xavicrack = etmp.getContent();

			for (Content ctmp : xavicrack) {

			    if (ctmp instanceof Text && control == 2) {

				System.out.println(((Text) ctmp).getText());
			    }
			}
			if (control == 2)
			    control = 0;
			else
			    control++;
			System.out.println("---------------------");
		    }

		    new File("tmp.xml").delete();
		} catch (Exception e1) {

		    e1.printStackTrace();
		}

	    }
	    // System.out.println(e.getChild("description").getText());
	    // break;
	}
	return null;
    }
}
